package com.jinke.api.common.annotation;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinke.api.common.exception.ApiException;
import com.jinke.api.common.exception.Asserts;
import com.jinke.api.modules.base.constant.RedisKeyConstants;
import com.jinke.api.modules.third.ezeeship.model.request.EstimateRateRequest;
import com.jinke.api.modules.ums.service.IApiLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * apilog 记日志切面
 */
@Aspect
@Component
@RequiredArgsConstructor
@Slf4j
public class ApilogHandler {

    @Resource
    private IApiLogService iApiLogService;


    @Pointcut("@annotation(com.jinke.api.common.annotation.Apilog)")
    public void pointcutOne() {

    }

    /**
     * 条件中必须有EstimateRateRequest
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("pointcutOne()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Apilog apilogAnnotation = method.getAnnotation(Apilog.class);
        String requestTypeName = apilogAnnotation.requestTypeName();
        String orderNO;
        // 获取参数中的 EstimateRateRequest 对象
        Object[] args = joinPoint.getArgs();
        EstimateRateRequest estimateRateRequest = null;

        for (Object arg : args) {
            if (arg instanceof EstimateRateRequest) {
                estimateRateRequest = (EstimateRateRequest) arg;
                break;
            }
        }
        if (estimateRateRequest != null) {
            // 修改注解中的 orderNo 值
            orderNO = estimateRateRequest.getOrderNO();

        } else {
            Object firstParam = joinPoint.getArgs()[1];
            orderNO = String.valueOf(firstParam);
        }



        Object retVal = joinPoint.proceed();
        if (retVal == null) {
            retVal = "返回为null";
        }

        iApiLogService.saveApiLog(retVal.toString(), orderNO, requestTypeName);
        return retVal;
    }
}
